#!/bin/bash
# PostgreSQL安装脚本
source ./postgresql_config.sh


### server PostgreSQL日志
SERVER_PSQL_LOG=$SERVER_LOG'/pgsql'
if [ ! -d ${SERVER_PSQL_LOG} ];then
echo_fun 2 服务器没有postgresql日志目录，创建目录${SERVER_PSQL_LOG}
mkdir -p ${SERVER_PSQL_LOG}
fi
### server PostgreSQL日志


##安装postgresql
SUCCESS=true


# 部署
echo_fun 1 $PLAT"上postgresql部署安装"


if [ $PLAT == "MACOSX" ];then
# start MACOSX

# 判断是否安装了postgresql
if [ ! -d $POSTGRESQL_INSTALL_PATH ];then
echo_fun 2 '使用brew安装postgresql到目录'$POSTGRESQL_INSTALL_PATH
brew install postgresql
# 判断执行成功没
if [ $? -ne 0 ];then
SUCCESS=false
fi # [ $? -ne 0 ];
else # [ ! -d $POSTGRESQL_INSTALL_PATH'*' ];
echo_fun 2 '已安装postgresql，跳过安装步骤'
SUCCESS=true
fi # [ ! -d $POSTGRESQL_INSTALL_PATH'*' ];

# end MACOSX
else
# start LINUX

#普通用户,用来启动postgresql数据库
PSQL_USER='postgres'

#postgresql安装包名
POSTGRESQL_VERSION='postgresql-11.2'

#存放二进制包的路径
SOFTWARE_PATH='/opt/software'


#postgresql源码编译
echo_fun 4 编译安装,等待时间较长. . .
cd $SOFTWARE_PATH
tar -xf ${POSTGRESQL_VERSION}.tar.gz
cd ${POSTGRESQL_VERSION}

#编译的时候可能需要解决下面2个依赖
yum install -y readline-devel
yum install zlib-devel;

#编译
./configure --prefix=${POSTGRESQL_INSTALL_PATH}
sleep 15s
make && make install
sleep 15s


#配置环境变量和指定PGDATA位置
echo_fun 4 配置环境变量和指定PGDATA位置
echo -e 'export POSTGRESQL_HOME='${POSTGRESQL_INSTALL_PATH}'\nexport PATH=${POSTGRESQL_HOME}/bin:$PATH\nexport PGDATA='${POSTGRESQL_INSTALL_PATH}'/data\n'>> /etc/profile
source /etc/profile


#创建用户,用来启动postgresql
echo_fun 4 创建用户,用来启动postgresql
useradd  ${PSQL_USER}
cd ${POSTGRESQL_INSTALL_PATH}/../
chown -R ${PSQL_USER}:${PSQL_USER}  ${POSTGRESQL_INSTALL_PATH}
fi # end LINUX


# 判断安装成功没有
if [ $SUCCESS == 'true' ]; then
echo_fun 2 安装postgresql成功
else # [ $SUCCESS == 'true' ];
echo_fun 3 安装postgresql失败
fi # [ $SUCCESS == 'false' ];


# 未配置数据库则配置数据库
path='/usr/local/data'
if [ ! -d $path ];then
sudo mkdir $path
fi

if [ ! -d $POSTGRESQL_DATABASE_PATH ];then
echo_fun 4 创建数据库目录$POSTGRESQL_DATABASE_PATH
sudo mkdir $POSTGRESQL_DATABASE_PATH
sudo chown -R -v ${PSQL_USER}:admin $POSTGRESQL_DATABASE_PATH
echo_fun 2 创建数据库目录成功


# su - ${PSQL_USER} <<EOF
# pg_ctl -D ${POSTGRESQL_DATABASE_PATH} initdb
# EOF
echo_fun 4 初始化数据库配置
echo_fun 4 登录${PSQL_USER}
su - ${PSQL_USER} -c "pg_ctl -D ${POSTGRESQL_DATABASE_PATH} initdb"
echo_fun 2 初始化数据库配置成功
# 设置权限
# sudo chmod -R 750 $POSTGRESQL_DATABASE_PATH


#修改postgresql的配置文件
echo_fun 2 修改postgresql的配置文件
cd ${POSTGRESQL_DATABASE_PATH}
echo_fun 2 修改配置文件pg_hba.conf
${SEDI} "s/127.0.0.1\/32            trust/0.0.0.0\/0               md5/g" pg_hba.conf
${SEDI} "s/::1\/128                 trust/::1\/0                   md5/g" pg_hba.conf
echo_fun 2 修改配置文件postgresql.conf
# 监听修改
${SEDI} "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" postgresql.conf
${SEDI} "s/#port = 5432/port = ${PGSQL_PORT}/g" postgresql.conf
# 日志修改
${SEDI} 's/#logging_collector = off/logging_collector = on/g' postgresql.conf
${SEDI} "s/#log_directory = 'log'/log_directory = '"${SERVER_PSQL_LOG//\//\\\/}"'/g" postgresql.conf
${SEDI} "s/#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'/log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'/g" postgresql.conf
${SEDI} "s/#log_truncate_on_rotation = off/log_truncate_on_rotation = off/g" postgresql.conf
${SEDI} "s/#log_rotation_age = 1d/log_rotation_age = 1d/g" postgresql.conf
${SEDI} "s/#log_rotation_size = 10MB/log_rotation_size = 10MB/g" postgresql.conf
echo_fun 4 修改postgresql的配置文件完成
fi # [ $SUCCESS == 'true' ];


# 判断是否启动 run_postgresql.sh
echo_fun 4 启动postgresql
psql_pid_num=`ps -ef |grep ${POSTGRESQL_DATABASE_PATH}|grep -v grep|wc -l`
if [ $psql_pid_num -eq 1 ];then
    echo_fun 4 已经有一个启动postgresql服务
else
    echo_fun 4 启动postgresql服务
    su - ${PSQL_USER} -c "pg_ctl -D ${POSTGRESQL_DATABASE_PATH} start"
fi

# 初始化PostgreSQL
cd ${SERVER_SCRIPTS}/pgsql
su - ${PSQL_USER} -c "cd $(pwd) && sh init_postgresql.sh"

